home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / public / bit / src / ulib / pof2.c < prev    next >
C/C++ Source or Header  |  1994-08-01  |  816b  |  39 lines

  1. /***********************************************************************
  2.  * $Id: pof2.c,v 0.80 1994/02/24 09:48:11 zhao Exp $
  3.  *
  4.  *.  Copyright(c) 1993,1994 by T.C. Zhao
  5.  *   All rights reserved.
  6.  *.
  7.  *  get the closest power of 2 no smaller than n
  8.  ***********************************************************************/
  9. #if !defined(lint) &&  defined(F_ID)
  10. char *id_pof2 = "$Id: pof2.c,v 0.80 1994/02/24 09:48:11 zhao Exp $";
  11. #endif
  12.  
  13. #include "ulib.h"
  14.  
  15. /* speed is of no concern */
  16. unsigned int
  17. power_of_2(register unsigned int n)
  18. {
  19.     register unsigned int i = 1;
  20.  
  21.     if (n == 0)
  22.     return 0;
  23.     while ((1 << i) < n)
  24.     i++;
  25.     return 1 << i;
  26. }
  27.  
  28. #ifdef TEST
  29. #include <stdio.h>
  30. main()
  31. {
  32.     int i;
  33.     while (fscanf(stdin, "%d", &i) == 1)
  34.     printf("Input= %d Pof2=%d\n", i, power_of_2(i));
  35.     return 0;
  36. }
  37.  
  38. #endif
  39.